package com.liu.netty.netty;

import com.liu.netty.pojo.Message;
import io.netty.channel.Channel;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class ClientManager {

    private static   ClientManager INSTANCE;

    private ClientManager(){

    }


    private String taskId = "";

    private  Integer taskSeq = 0;

    public String getTaskId() {
        if(taskSeq == 99999) {
            taskSeq = 0;
        }else {
            taskSeq = taskSeq +1;
        }
        taskId = taskSeq +"";
        int cycle = 0;
        if(taskId.length() < 5) {
            cycle = 5 - taskId.length();
        }
        for(int i=0;i<cycle;i++) {
            taskId = "0" + taskId;
        }
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public Integer getTaskSeq() {
        return taskSeq;
    }

    public void setTaskSeq(Integer taskSeq) {
        this.taskSeq = taskSeq;
    }


    static {
        INSTANCE = new ClientManager();
    }

   public static Map<String, Message> taskMap = new ConcurrentHashMap<String, Message>();

   private static final Map<String, Channel> clientSession =new ConcurrentHashMap<String,Channel>();


   public static void addClient(String clientId,Channel Channel){

        clientSession.put(clientId,Channel);
   }

   public static Channel getChannelId(String clientId){
       return  clientSession.get(clientId);
   }

    public static void removeChannelId(String clientId){
        clientSession.remove(clientId);
    }

   public static  Map getAllClient(){
       return  clientSession;
   }


    public static ClientManager getINSTANCE() {
        return INSTANCE;
    }


}
